package com.crl.rcdp.monitor.controller;

import com.cars.util.json.JsonResult;
import com.cars.util.json.JsonResultUtil;
import com.cars.util.json.JsonUtil;
import com.crl.rcdp.monitor.model.MonitorService;
import com.crl.rcdp.monitor.service.MonitorEurekaService;
import com.crl.rcdp.monitor.vo.MonitorEurekaVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: 孔垂云
 * @version: 1.0
 * @date: 2020/8/9 1:08
 */
@RestController
@RequestMapping("/monitor/eureka")
public class MonitorEurekaController {
    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private MonitorEurekaService monitorEurekaService;

    /**
     * 获取注册在Eureka中的服务名称
     *
     * @return
     */
    @PostMapping("/getEurekaServices")
    public JsonResult getEurekaServices() {
        List<MonitorService> list = new ArrayList<>();
        List<String> serviceNames = discoveryClient.getServices();
        for (String serviceName : serviceNames) {
            List<ServiceInstance> serviceInstances = discoveryClient.getInstances(serviceName);
            for (ServiceInstance serviceInstance : serviceInstances) {
                //services.add(String.format("%s:%s", serviceName, serviceInstance.getUri()));
                MonitorService monitorService = new MonitorService();
                monitorService.setServiceName(serviceName);//服务名
                monitorService.setUri(serviceInstance.getUri().toString());//uri
                monitorService.setInstanceId(serviceInstance.getInstanceId());//实例名
//                monitorService.setStatus(serviceInstance);
                list.add(monitorService);
                Map<String, Object> map = JsonUtil.toMap(JsonUtil.toStr(serviceInstance));
                map.forEach((key, value) -> {
//                    System.out.println("Key: " + key + ", Value: " + value);
                });
//                System.out.println(map.toString());
                Map<String, Object> mapInstance = JsonUtil.toMap(JsonUtil.toStr(map.get("instanceInfo")));
                monitorService.setStatus(mapInstance.get("status").toString());//状态
                Map<String, Object> mapLease = JsonUtil.toMap(JsonUtil.toStr(mapInstance.get("leaseInfo")));
                monitorService.setRegistTime(long2date(Long.parseLong(mapLease.get("registrationTimestamp").toString())));//注册时间
                monitorService.setRenewTiem(long2date(Long.parseLong(mapLease.get("lastRenewalTimestamp").toString())));//续约时间
                monitorService.setServiceUpTime(long2date(Long.parseLong(mapLease.get("serviceUpTimestamp").toString())));//上线时间
//                System.out.println(serviceInstance.toString());
            }
        }
        return JsonResultUtil.ok(list);
    }

    private Date long2date(long timestamp) {
        Date mydate = new Date();
        mydate.setTime(timestamp);
        return mydate;
    }

    /**
     * 上线
     *
     * @param monitorEurekaVO
     * @return
     */
    @PostMapping("/up")
    @ApiOperation(value = "启动", notes = "启动", response = JsonResult.class)
    public JsonResult up(@RequestBody MonitorEurekaVO monitorEurekaVO) {
        monitorEurekaService.up(monitorEurekaVO);
        return JsonResultUtil.ok();
    }

    /**
     * 下线
     *
     * @param monitorEurekaVO
     * @return
     */
    @PostMapping("/down")
    @ApiOperation(value = "下线", notes = "下线", response = JsonResult.class)
    public JsonResult down(@RequestBody MonitorEurekaVO monitorEurekaVO) {
        monitorEurekaService.down(monitorEurekaVO);
        return JsonResultUtil.ok();
    }
}
